package com.business.api.official.toursism.websitehotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.api.official.toursism.websitehotel.dto.WebsiteHotelApiDTO;
import com.business.api.official.toursism.websitehotel.from.WebsiteHotelApiSaveFrom;
import com.business.api.official.toursism.websitehotel.from.WebsiteHotelApiUpdateFrom;
import com.business.api.official.toursism.websitehotel.manager.WebsiteHotelApiManager;
import com.business.api.official.toursism.websitehotel.query.WebsiteHotelApiPageQuery;
import com.business.api.official.toursism.websitehotel.query.WebsiteHotelApiQuery;
import com.business.api.official.toursism.websitehotel.service.WebsiteHotelApiService;
import com.business.api.official.toursism.websitehotel.vo.WebsiteHotelApiBriefPageVO;
import com.business.api.official.toursism.websitehotel.vo.WebsiteHotelApiDetailListVO;
import com.business.api.official.toursism.websitehotel.vo.WebsiteHotelApiDetailPageVO;
import com.business.api.official.toursism.websitehotel.vo.data.WebsiteHotelApiBriefData;
import com.business.api.official.toursism.websitehotel.vo.data.WebsiteHotelApiDetailData;
import com.business.system.official.toursism.websitehotel.entity.WebsiteHotel;
import com.business.system.official.toursism.websitehotel.service.WebsiteHotelService;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.dto.SeedMessageDTO;
import com.seed.core.pojo.service.impl.SeedApiServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description 乌镇酒店
 * @author CodeGenerator
 * @date   2025-01-25
 * @version V1.0
 */
@Slf4j
@Service("websiteHotelApiService")
@Transactional(rollbackFor = ServiceException.class)
public class WebsiteHotelApiServiceImpl extends SeedApiServiceImpl<WebsiteHotelApiManager, WebsiteHotel> implements WebsiteHotelApiService {

    @Autowired
    private WebsiteHotelService systemService;


    /**
     * 持久化
     *
     * @param websiteHotelFrom 需要保存的对象
     * @return id，如果保存失败返回空字符串
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteHotelApiDTO save(WebsiteHotelApiSaveFrom websiteHotelFrom) throws ServiceException {
        WebsiteHotelApiDTO apiDTO = new WebsiteHotelApiDTO();
        WebsiteHotel websiteHotel = new WebsiteHotel();
        try {
            BeanUtils.copyProperties(websiteHotelFrom, websiteHotel, "id");
        } catch (Exception e) {
            throw new ServiceException(e, websiteHotelFrom);
        }
        SeedMessageDTO baseSeedDTO = this.save(websiteHotel);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        apiDTO.setWebsiteHotel(websiteHotel);
        return apiDTO;
    }

    /**
     * 部分更新
     *
     * @param websiteHotelFrom 需要更新的对象
     * @return 是否成功
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteHotelApiDTO updateSelectiveById(WebsiteHotelApiUpdateFrom websiteHotelFrom) throws ServiceException {
        WebsiteHotelApiDTO apiDTO = new WebsiteHotelApiDTO();
        if (StringUtils.isEmpty(websiteHotelFrom.getId())) {
            apiDTO.setCode("A0410");
            apiDTO.setMessage("更新主键为空");
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        WebsiteHotel websiteHotel = new WebsiteHotel();
        try {
            BeanUtils.copyProperties(websiteHotelFrom, websiteHotel);
        } catch (Exception e) {
            throw new ServiceException(e, websiteHotelFrom);
        }
        SeedMessageDTO baseSeedDTO = this.updateById(websiteHotel);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        return apiDTO;
    }

    /**
     * 获取全部
     *
     * @param query 查询条件
     * @return 详情VOList
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteHotelApiDetailListVO listAll(WebsiteHotelApiQuery query) throws ServiceException {
        WebsiteHotel queryEntity = new WebsiteHotel();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteHotel> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        List<WebsiteHotel> list = this.list(lambdaQueryWrapper);
        List<WebsiteHotelApiDetailData> detailVOList;
        try {
            detailVOList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteHotelApiDetailListVO(detailVOList);
    }

    /**
     * 简略对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的简略对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteHotelApiBriefPageVO getBriefPageVO(WebsiteHotelApiPageQuery query) throws ServiceException {
        Page<WebsiteHotel> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteHotel queryEntity = new WebsiteHotel();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteHotel> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteHotel> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteHotel> list = pageData.getRecords();
        List<WebsiteHotelApiBriefData> voList;
        try {
            voList = baseManager.listConvertApiBriefData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteHotelApiBriefPageVO(page, voList);
    }

    /**
     * 详细对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的详细对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteHotelApiDetailPageVO getDetailPageVO(WebsiteHotelApiPageQuery query) throws ServiceException {
        Page<WebsiteHotel> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteHotel queryEntity = new WebsiteHotel();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteHotel> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteHotel> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteHotel> list = pageData.getRecords();
        List<WebsiteHotelApiDetailData> voList;
        try {
            voList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteHotelApiDetailPageVO(page, voList);
    }

}